﻿import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Particles 2.12
import QtGraphicalEffects 1.12

import "../components"
import "../analysis"

Item {

    anchors.fill: parent
    anchors.margins: 15 *dp


    function refresh(){
        cDeviceList.refresh()
        cExperimentStep.refresh()
    }



    // 样式 =============================================================================================
    Component.onCompleted: {
        // 设备筛选样式修改
        var filterList = layout_deviceFilter.children
        for(let i = 0; i < filterList.length; i ++){
            filterList[i].button_color = "transparent"
            filterList[i].text_color = "#999999"
            filterList[i].text_color_select = "#4488EE"
            filterList[i].button_color_disable = "transparent"
            filterList[i].button_border_disable = "transparent"
            filterList[i].text_color_disable = "#999999"
        }
    }

    Item{                           // 上面的条
        id: layout_top
        height: 40 *dp
        width: parent.width
        Text{
            y: -5*dp
            text: gPageModel===0 ? "运行实验" : "新建实验"
            font.pixelSize: config.fontsize+4
            font.bold: true
            anchors.verticalCenter: parent.verticalCenter
        }
        Row{
            id: layout_deviceFilter
            y: 33*dp
            spacing: 5
            PushButton{
                height: 20*dp
                width: 94*dp
                title: "当前可用设备: " + gCountAvailable
                text_size: config.fontsize - 2
                visible: gPageModel === 1
                onReleased: {
                }
            }
            PushButton{
                height: 20*dp
                width: 68*dp
                title: "设备总数: " + gCountAll
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 5
                isDisabled: gCountAll === 0
                onReleased: {
                    changeDeviceFilter(5)
                }
            }
            PushButton{
                height: 20*dp
                width: 55*dp
                title: "  在线: "+gCountEmpty
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 1
                isDisabled: gCountEmpty === 0
                onReleased: {
                    changeDeviceFilter(1)
                }
            }PushButton{
                height: 20*dp
                width: 60*dp
                title: "运行中: " + gCountRunning
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 2
                isDisabled: gCountRunning === 0
                onReleased: {
                    changeDeviceFilter(2)
                }
            }PushButton{
                height: 20*dp
                width: 60*dp
                title: "已完成: " + gCountFinish
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 3
                isDisabled: gCountFinish === 0
                onReleased: {
                    changeDeviceFilter(3)
                }
            }PushButton{
                height: 20*dp
                width: 50*dp
                title: "故障: " + gCountFault
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 4
                isDisabled: gCountFault === 0
                onReleased: {
                    changeDeviceFilter(4)
                }
            }PushButton{
                height: 20*dp
                width: 50*dp
                title: "离线: " + gCountOffline
                text_size: config.fontsize - 2
                visible: gPageModel === 0
                isSelected: gDeviceFilter === 0
                isDisabled: gCountOffline === 0
                onReleased: {
                    changeDeviceFilter(0)
                }
            }
        }

        PushButton{
            title: "一键启动"
            x: parent.width - 390*dp;   y: 5*dp
            defaultMode2: true
            button_icon: "qrc:/picture/lighting.svg"
            icon_color: "#1677FF"
            visible: gPageModel === 0
            isDisabled: gCurDeviceStatus === 0 || gCurDeviceStatus === 2 || gCurDeviceId === "" || gCurDeviceStatus === 4
            onReleased: {
                if(gComboModel_program.length === 0){
                    boxwarning_noAmplProgram.show()
                }else{
                    onButtonOneStart()
                }
            }onReleased_disabled: {
                mainwindow.showToast("请切换空闲中设备", 2)
            }
        }PushButton{
            title: "打开实验"
            x: parent.width - 255*dp;   y: 5*dp
            defaultMode2: true
            button_icon: "qrc:/picture/openExperiment.svg"
            icon_color: "#1677FF"
            isDisabled: gCountAvailable <= 0
            visible: gPageModel === 0
            onReleased: {
                onButtonOpenExp()
            }onReleased_disabled: {
                mainwindow.showToast("当前没有可用设备", 2)
            }


        }PushButton{
            x: parent.width - 120*dp;   y: 5*dp
            title: "新建实验"
            defaultMode1: true
            button_icon: "qrc:/picture/newExperiment.png"
            icon_color: "white"
            isDisabled: gCountAvailable <= 0
            visible: gPageModel === 0
            onReleased: {
                if(gComboModel_program.length === 0){
                    boxwarning_noAmplProgram.show()
                }else{
                    onButtonNewExp()
                }

            }onReleased_disabled: {
                mainwindow.showToast("当前没有可用设备", 2)
            }
        }PushButton{
            y: 5*dp
            x: parent.width - 120*dp
            title: "返回"
            defaultMode2: true
            visible: gPageModel === 1
            onReleased: {
                onButtonBack()
            }
        }
    }



    Item{                           // 左侧设备列表
        y: 60 *dp
        width: 170 *dp
        height: parent.height-100*dp
        DeviceList{
            id: cDeviceList
        }
    }
    Item{                   // 设备列表下面的开关仓门
        y: parent.height - 40*dp
        visible: gCurDeviceId !== "" && gPageModel === 0
        width: 170*dp
        height: 40*dp
        PushButton{
            title: "开仓"
            width: 80*dp
            defaultMode1: true
            button_icon: "qrc:/picture/door.svg"
            isDisabled: gCurDeviceStatus===0 || gCurDeviceStatus===2 || gCurDeviceStatus===4
            icon_color: "white"
            onReleased: {
                CExpInfo.openDoor(gCurDeviceId)
                console.log("=== 仓门"+gCurDeviceId+"已开启")
            }
        }PushButton{
            title: "关仓"
            x: 90*dp
            width: 80*dp
            defaultMode2: true
            button_icon: "qrc:/picture/door.svg"
            isDisabled: gCurDeviceStatus===0 || gCurDeviceStatus===2 || gCurDeviceStatus===4
            icon_color: "#1677FF"
            onReleased: {
                CExpInfo.closeDoor(gCurDeviceId)
                console.log("=== 仓门"+gCurDeviceId+"已关闭")
            }
        }
    }

    Rectangle{                           // 右侧实验信息
        x: 180*dp
        y: 60*dp
        width: parent.width-180*dp
        height: parent.height-60*dp
        radius: 10*dp

        ExperimentStep{
            id: cExperimentStep
            visible: gPageModel === 1 || (gCurDeviceStatus > 1 && gCurDeviceId!=="" && gCurDeviceStatus !== 4)
            onVisibleChanged: {
            }
        }
        Item{
            visible: (gCurDeviceStatus <=1 || gCurDeviceStatus === 4) && gPageModel === 0
            anchors.fill: parent
            Image{
                width: 300*dp
                height: 280*dp
                anchors.centerIn: parent
                anchors.verticalCenterOffset: -100*dp
                source: gCurDeviceStatus===0 ? "qrc:/picture/offline.png" : "qrc:/picture/free.png"
                Text{
                    text:  gCurDeviceStatus===0 ? "请检查设备是否启动或网络连接情况" : (gCurDeviceStatus === 4 ? "当前设备存在故障，请检查并重新连接设备" : "当前设备空闲中，快去创建新实验吧~")
                    font.pixelSize: config.fontsize + 2
                    anchors.horizontalCenter: parent.horizontalCenter
                    y: 280*dp
                    color: "#AAAAAA"
                }
                PushButton{
                    visible: gCurDeviceStatus === 0 || gCurDeviceStatus === 4
                    anchors.horizontalCenter: parent.horizontalCenter
                    y: 340*dp
                    text_color: "#666666"
                    title: "设备管理"
                    defaultMode2: true
                    onReleased: {
                        onButtonDeviceManager()
                    }
                }
            }
        }


    }
    Rectangle{                           // 设备为空时显示的界面
        visible: gCurDeviceId === ""
        x: 0
        y: 60*dp
        width: parent.width
        height: parent.height - 60*dp
        color: "#F7F7FA"
        Image{
            width: 300*dp
            height: 280*dp
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -100*dp
            source: "qrc:/picture/empty.png"
            Text{
                text: "当前没有可用的设备，请检查设备连接！"
                font.pixelSize: config.fontsize + 2
                anchors.horizontalCenter: parent.horizontalCenter
                y: 280*dp
                color: "#AAAAAA"
            }
            // 按钮-粒子版
            Rectangle{
                y: 340*dp
                height: 40*dp
                width: 120*dp
                anchors.horizontalCenter: parent.horizontalCenter
                radius: 12*dp
                clip: true
                LinearGradient {            ///--[Mark]
                    anchors.fill: parent
                    start: Qt.point(0, 0)
                    end: Qt.point(width, 0)      ///1.横向渐变
                    gradient: Gradient {
                        GradientStop {  position: 0.0;    color: "#EC77AB" }
                        GradientStop {  position: 0.5;    color: "#AE75D2" }
                        GradientStop {  position: 1.0;    color: "#7973F4" }
                    }
                }
                Text{
                    anchors.centerIn: parent
                    color: "white"
                    font.pixelSize: config.fontsize
                    text: "添加设备"
                }
                MouseArea{
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                    hoverEnabled: true
                    onEntered: {
                        emitter.emitRate = 20
                    }onExited: {
                        emitter.emitRate = 0
                    }
                    onReleased: {

                        onButtonDeviceManager()
                    }
                }

                ParticleSystem {
                    id: particleSystem;
                }

                Emitter {
                    id: emitter;
                    anchors.fill: parent
                    system: particleSystem;
                    emitRate: 0;
                    lifeSpan: 800;
                    lifeSpanVariation: 300;
                    size: 3
                    endSize: 3
                }

                ImageParticle {
                    system: particleSystem;
                    source: "qrc:/picture/star.png"
                }
            }
        }
    }
}
